/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SocketBusiness;

import CommonValue.SystemTimer;
import DTO.ControlDTO;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class BossOrderSlave extends ClientConnect {

    ControlDTO _controlSlave;
    ControlDTO _lastControlSlave;

    public BossOrderSlave(String systemSign, String ServerIP, int port, ControlDTO operation, ControlDTO _controlSlave) {
        super(systemSign, ServerIP, port, operation);
        this._controlSlave = _controlSlave;
        this._lastControlSlave = new ControlDTO(_controlSlave.getBoss(), _controlSlave.getSlave());
    }

//    public BossOrderSlave(String systemSign, String ServerIP, int port, ControlDTO _controlSlave) {
//        super(systemSign, ServerIP, port,);
//        this._controlSlave = _controlSlave;
//        this._lastControlSlave = new ControlDTO(_controlSlave.getBoss(), _controlSlave.getSlave());
//    }
    @Override
    public void business() {
        boolean finish = false;
        do {
            try {
                Thread.sleep(SystemTimer.CheckControlSleep);
            } catch (InterruptedException ex) {
                Logger.getLogger(BossOrderSlave.class.getName()).log(Level.SEVERE, null, ex);
            }

            synchronized (_controlSlave) {
                if (_lastControlSlave.getFlag() != _controlSlave.getFlag()) {
                    try {
                        this.sendMessage(_controlSlave);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(BossOrderSlave.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    _lastControlSlave.setFlag(_controlSlave.getFlag());
                    System.out.println("DA GUI TIN HIEU " + (_controlSlave.getFlag() ? "ON" : "OFF") + " TU HE THONG " + _controlSlave.getBoss() + "TOI 1 HE THONG SLAVE : " + _controlSlave.getSlave());
                }
            }

        } while (!finish);
    }
}
